/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ssau.customerManagement.view;

import java.awt.event.ItemEvent;
import org.apache.commons.lang.StringUtils;
import ru.ssau.customerManagement.controller.Controller;
import ru.ssau.customerManagement.controller.CreationException;
import ru.ssau.customerManagement.controller.NotificationListener;
import ru.ssau.customerManagement.controller.remoting.ClientController;
import ru.ssau.customerManagement.model.entities.Customer;
import ru.ssau.customerManagement.model.entities.CustomerLocation;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author illabefat
 */
public class CustomerManagementForm extends javax.swing.JFrame implements NotificationListener {

    /**
     * Creates new form CustomerManagementForm
     */
    public CustomerManagementForm(Controller controller) {
        initComponents();
        this.controller = controller;

        jComboBox1.setModel(new DefaultComboBoxModel(getCustomerIds()));
        jComboBox1.setSelectedItem(null);
        jComboBox4.setModel(new DefaultComboBoxModel(new Long[0]));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();
        jPanel1 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        jPanel4 = new javax.swing.JPanel();
        jComboBox4 = new javax.swing.JComboBox();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jTextField10 = new javax.swing.JTextField();
        jTextField11 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jToggleButton2 = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Customers"));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jButton1.setText("New");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Delete");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("id");

        jLabel2.setText("Name");

        jLabel3.setText("Description");

        jTextField1.setEnabled(false);
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        jTextField2.setEnabled(false);

        jTextField3.setEnabled(false);

        jButton3.setText("Save");
        jButton3.setEnabled(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jToggleButton1.setText("Edit");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 155, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(46, 46, 46)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField1)
                    .addComponent(jTextField2)
                    .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton2))
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jToggleButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3))))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Customer Locations"));

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox4.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox4ItemStateChanged(evt);
            }
        });

        jButton7.setText("New");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setText("Delete");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jLabel10.setText("id");

        jLabel11.setText("Address");

        jTextField10.setEnabled(false);
        jTextField10.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField10KeyTyped(evt);
            }
        });

        jTextField11.setEnabled(false);

        jButton4.setText("Save");
        jButton4.setEnabled(false);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jToggleButton2.setText("Edit");
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 155, Short.MAX_VALUE)
                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton8))
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addGap(59, 59, 59)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToggleButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton8))
                    .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(jToggleButton2, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel11)
                        .addComponent(jButton4))
                    .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        jTextField1.setEnabled(true);
        jTextField2.setEnabled(true);
        jTextField3.setEnabled(true);
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jComboBox1.setSelectedItem(null);
        jButton3.setEnabled(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Long id = (Long) jComboBox1.getSelectedItem();
        controller.deleteCustomer(id);

        jTextField1.setEnabled(false);
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        List<Customer> customers = controller.getCustomers();
        Long[] ids = new Long[customers.size()];
        for (int i=0; i< ids.length; i++)
        {
            ids[i] = customers.get(i).getId();
        }
        jComboBox1.setModel(new DefaultComboBoxModel(ids));
        jComboBox1.setSelectedItem(null);

        jTextField10.setEnabled(false);
        jTextField10.setText("");
        jTextField11.setText("");
        jComboBox4.setModel(new DefaultComboBoxModel(new Long[0]));
        jComboBox4.setSelectedItem(null);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        jTextField10.setEnabled(true);
        jTextField11.setEnabled(true);
        jTextField10.setText("");
        jTextField11.setText("");
        jButton4.setEnabled(true);
        jComboBox4.setSelectedItem(null);
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        // Delete location button
        Long id = (Long) jComboBox4.getSelectedItem();
        controller.deleteCustomerLocation(id);

        jTextField10.setEnabled(false);
        jTextField10.setText("");
        jTextField11.setText("");

        Long customerId = Long.valueOf(jTextField1.getText());
        List<CustomerLocation> customerLocations = controller.getLocationsByCustomer(customerId);
        Long[] ids = new Long[customerLocations.size()];
        for (int i=0; i< ids.length; i++)
        {
            ids[i] = customerLocations.get(i).getId();
        }
        jComboBox4.setModel(new DefaultComboBoxModel(ids));
        jComboBox4.setSelectedItem(null);
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        String idString = jTextField1.getText();
        if (StringUtils.isEmpty(idString))
            return;
        Long id = Long.valueOf(idString);
        String name = jTextField2.getText();
        String description = jTextField3.getText();
        Customer cust = new Customer(id, name, description);

        if (jTextField1.isEnabled())
        {
            try
            {
                controller.addCustomer(cust);
            }
            catch (CreationException e)
            {
                JOptionPane.showMessageDialog(this, e.getMessage());
                return;
            }
        }
        else
        {
            controller.replaceCustomer(cust);
            controller.unlockCustomer((Long) jComboBox1.getSelectedItem());
        }
        jTextField1.setEnabled(false);
        jTextField2.setEnabled(false);
        jTextField3.setEnabled(false);
        jButton3.setEnabled(false);
        jToggleButton1.setSelected(false);

        List<Customer> customers = controller.getCustomers();
        Long[] ids = new Long[customers.size()];
        for (int i=0; i< ids.length; i++)
        {
            ids[i] = customers.get(i).getId();
        }
        jComboBox1.setModel(new DefaultComboBoxModel(ids));
        jComboBox1.setSelectedItem(id);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // Save location
        String idString = jTextField10.getText();
        if (StringUtils.isEmpty(idString))
            return;
        Long id = Long.valueOf(idString);
        Long customerId = Long.valueOf(jTextField1.getText());
        String address = jTextField11.getText();
        CustomerLocation customerLocation = new CustomerLocation(id, customerId, address);

        if (jTextField10.isEnabled())
        {
            try
            {
                controller.addCustomerLocation(customerLocation);
            }
            catch (CreationException e)
            {
                JOptionPane.showMessageDialog(this, e.getMessage());
                return;
            }
        }
        else
        {
            controller.replaceCustomerLocation(customerLocation);
            controller.unlockCustomerLocation((Long) jComboBox1.getSelectedItem());
        }
//        List<CustomerLocation> customerLocations = controller.getLocationsByCustomer(customerId);
//        Long[] ids = new Long[customerLocations.size()];
//        for (int i=0; i< ids.length; i++)
//        {
//            ids[i] = customerLocations.get(i).getId();
//        }
        jButton4.setEnabled(false);
        //jComboBox4.setModel(new DefaultComboBoxModel(ids));
        jComboBox4.setSelectedItem(id);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
        if (!Character.isDigit(evt.getKeyChar()))
        {
            evt.consume();
        }
    }//GEN-LAST:event_jTextField1KeyTyped

    private void jTextField10KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField10KeyTyped
        if (!Character.isDigit(evt.getKeyChar()))
        {
            evt.consume();
        }
    }//GEN-LAST:event_jTextField10KeyTyped

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        // Lock customer
        if (jToggleButton1.isSelected())
        {
            boolean res = controller.tryLockCustomer((Long) jComboBox1.getSelectedItem());
            if (!res)
            {
                JOptionPane.showMessageDialog(this, "Customer is being edited by another user");
                jToggleButton1.setSelected(false);
            }
            else
            {
                jTextField2.setEnabled(true);
                jTextField3.setEnabled(true);
                jButton3.setEnabled(true);
            }
        }
        else
        {
            controller.unlockCustomer((Long) jComboBox1.getSelectedItem());
            jTextField2.setEnabled(false);
            jTextField3.setEnabled(false);
            jButton3.setEnabled(false);
        }
    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton2ActionPerformed
        // Lock customerLocation
        if (jToggleButton2.isSelected())
        {
            boolean res = controller.tryLockCustomerLocation((Long) jComboBox4.getSelectedItem());
            if (!res)
            {
                jToggleButton2.setSelected(false);
                JOptionPane.showMessageDialog(this, "CustomerLocation is being edited by another user");
            }
            else
            {
                jTextField11.setEnabled(true);
                jButton4.setEnabled(true);
            }
        }
        else
        {
            controller.unlockCustomerLocation((Long) jComboBox1.getSelectedItem());
            jTextField11.setEnabled(false);
            jButton4.setEnabled(false);
        }
    }//GEN-LAST:event_jToggleButton2ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        if (evt.getStateChange() != ItemEvent.SELECTED) {
          return;
       }

        Long id = (Long) jComboBox1.getSelectedItem();
        if (id == null)
        {
            jComboBox4.setModel(new DefaultComboBoxModel(new Long[0]));
            jTextField10.setEnabled(false);
            return;
        }
        Customer customer = controller.getCustomer(id);
        jTextField1.setEnabled(false);
        jTextField1.setText(customer.getId().toString());
        jTextField2.setText(customer.getName());
        jTextField3.setText(customer.getDescription());

        List<CustomerLocation> locations = controller.getLocationsByCustomer(id);
        Long[] locationIds = new Long[locations.size()];
        for (int i =0; i< locationIds.length; i++)
        {
            locationIds[i] = locations.get(i).getId();
        }
        jComboBox4.setModel(new DefaultComboBoxModel(locationIds));
        jComboBox4.setSelectedItem(null);
        jTextField10.setEnabled(false);
        jTextField10.setText("");
        jTextField11.setText("");
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jComboBox4ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox4ItemStateChanged
        if (evt.getStateChange() != ItemEvent.SELECTED) {
          return;
        }
        
        Long id = (Long) jComboBox4.getSelectedItem();
        if (id == null)
            return;
        CustomerLocation loc = controller.getLocation(id);
        jTextField10.setEnabled(false);
        jTextField11.setEnabled(false);
        jTextField10.setText(loc.getId().toString());
        jTextField11.setText(loc.getAddress());
    }//GEN-LAST:event_jComboBox4ItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;
    // End of variables declaration//GEN-END:variables
    private Controller controller;

    @Override
    public void customerChanged(Customer customer) {
        Long customerId = (Long) jComboBox1.getSelectedItem();
        if (customer.getId().equals(customerId))
        {
            //do magic
        }
    }

    @Override
    public void customerAdded(Customer customer) {
        ((DefaultComboBoxModel)jComboBox1.getModel()).addElement(customer.getId());
    }

    @Override
    public void customerRemoved(Long id) {
        ((DefaultComboBoxModel)jComboBox1.getModel()).removeElement(id);
    }

    @Override
    public void customerLocationChanged(CustomerLocation location) {
        Long locationId = (Long) jComboBox4.getSelectedItem();
        if (location.getId().equals(locationId))
        {
            //do magic
        }
    }

    @Override
    public void customerLocationAdded(CustomerLocation location) {
        Long customerId = (Long) jComboBox1.getSelectedItem();
        if (location.getCustomerId().equals(customerId))
        {
            ((DefaultComboBoxModel)jComboBox4.getModel()).addElement(location.getId());
        }
    }

    @Override
    public void customerLocationRemoved(Long id) {
        ((DefaultComboBoxModel)jComboBox4.getModel()).removeElement(id);
    }


    private Long[] getCustomerIds()
    {
        List<Customer> customers = controller.getCustomers();
        Long[] ids = new Long[customers.size()];
        for (int i=0; i< ids.length; i++)
        {
            ids[i] = customers.get(i).getId();
        }

        return ids;
    }
}
